package org.taobao.ibatiside.ui.wizard;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.taobao.ibatiside.model.mapperv2.Mapperv2Factory;
import org.taobao.ibatiside.model.mapperv2.SqlMap;
import org.taobao.ibatiside.ui.util.Config2XMLProcessor;
import org.taobao.ibatiside.ui.util.Mapper2XMLProcessor;
import org.taobao.ibatiside.ui.util.WorkspaceUtil;

public class OverViewNewMapperWizard extends Wizard {

	private NewMapperFilePage settings;
	private final IFile configFile;
	private IFile sqlMapFile;
	public OverViewNewMapperWizard(IFile config) {
		super();
		this.configFile = config;
		setNeedsProgressMonitor(true);
	}
	public IFile getSqlMapFile() {
		return sqlMapFile;
	}
	@Override
	public void addPages() {
		settings = new NewMapperFilePage();
		settings.setConfigurationFile(this.configFile);
		addPage(settings);
	}
	@Override
	public boolean performFinish() {
		final IPath map_file_path = settings.getNewMapperPath();
		try {
			getContainer().run(false, false, new IRunnableWithProgress() {
				
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
					sqlMapFile = createMapFile(map_file_path);
					//create map first, then update config
					if (sqlMapFile == null) {
						throw new InterruptedException("can't create sqlmap file:"+map_file_path);
					}
					try {
						sqlMapFile.getParent().refreshLocal(IResource.DEPTH_ZERO, monitor);
						updateConfig(sqlMapFile);
					} catch (CoreException e) {
						throw new InvocationTargetException(e);
					}
				}
			});
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	protected IFile createMapFile(IPath map_file_path) {
		SqlMap one_map = Mapperv2Factory.eINSTANCE.createSqlMap();
		one_map.setNamespace(settings.getNamespace());
		one_map.getTypeAlias().addAll(settings.getAliases());
		Mapper2XMLProcessor processor = new Mapper2XMLProcessor();
		boolean saved = processor.save2File(one_map, map_file_path);
		if (saved){
			return ResourcesPlugin.getWorkspace().getRoot().getFile(map_file_path);
		}
		return null;
	}
	protected void updateConfig(IFile map_file) {
		Config2XMLProcessor process = new Config2XMLProcessor();
		IPath pkg_path = WorkspaceUtil.getPackageFragementPath(map_file.getFullPath());
		process.addSqlMap(configFile, pkg_path.toString());
		try {
			configFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	
}
